package elitech.vietnam.weatherfashion.notification;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.Notify;
import elitech.vietnam.weatherfashion.notification.NotificationEndlessListView.EndlessListener;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;

public class NotificationPopupWindow extends PopupWindow implements EndlessListener {

	Context mContext;
	NotificationEndlessListView mListView;
	TextView mTextView;
	ProgressBar mProgress;
	NotificationArrayAdapter mAdapter;
	
	public NotificationPopupWindow(Context context) {
		super(context);
		mContext = context;
		
		setContentView(LayoutInflater.from(context).inflate(R.layout.notification_window, null));
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.MATCH_PARENT);
		
		mListView = (NotificationEndlessListView) getContentView().findViewById(R.id.notiListview);
		mTextView = (TextView) getContentView().findViewById(R.id.notiTextNoData);
		mProgress = (ProgressBar) getContentView().findViewById(R.id.notiProgress);
		
		mListView.setVerticalScrollBarEnabled(false);
//		mListView.setLoadingView(R.layout.layout_loadmore);
		mAdapter = new NotificationArrayAdapter(mContext, R.layout.notification_list_item, 0, WFApplication.getInstance().mListNoti);
		mListView.setAdapter(mAdapter);
		mListView.setListener(this);
		mListView.setOnItemClickListener(mAdapter);
		
		if (mAdapter.getCount() < Const.NOTIFICATION_PER_LOAD)
			endOfList();
		
//		setOutsideTouchable(true);
		setFocusable(true);
	}
	
	public void notifyDataChanged() {
		mAdapter.notifyDataSetChanged();
	}
	
	class GetNotifications extends AsyncTask<Integer, Integer, Boolean> {
		List<Notify> rs;
		@Override
		protected Boolean doInBackground(Integer... params) {
			PHPSoapServiceCaller service = new PHPSoapServiceCaller();
			int memID = params[0];
			try {
				rs = service.getNotificationUser(memID, WFApplication.getInstance().mListNoti.size(), Const.NOTIFICATION_PER_LOAD);
			} catch (IOException e) {
				e.printStackTrace();
				rs = new ArrayList<Notify>();
			}
			return null;
		}
		@Override
		protected void onPreExecute() {
			mListView.loadMore(true);
		}
		@Override
		protected void onPostExecute(Boolean result) {
			mListView.loadMore(false);
			WFApplication.getInstance().mListNoti.addAll(rs);
			mAdapter.notifyDataSetChanged();
			if (rs.size() < Const.NOTIFICATION_PER_LOAD)
				endOfList();
		}
	}
	
	@Override
	public void loadData() {
		MemberDTO mem = GlobalValue.getInstance(mContext).readUserInfo();
		new GetNotifications().execute(mem.getId());
	}
	
	public void endOfList() {
		mListView.endOfList();
	}
}
